//
//  LinearInterpView.m
//  SlideMenu
//
//  Created by khoa nguyen on 7/24/14.
//  Copyright (c) 2014 Aryan Ghassemi. All rights reserved.
//

#import "LinearInterpView.h"

@implementation LinearInterpView{
    UIBezierPath *path; // (3)
}

int shapeIndex;
CGPoint first_point;
CGPoint last_point;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }

    shapeIndex= 1;
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder // (1)
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setMultipleTouchEnabled:NO]; // (2)
        self.opaque = NO;
        //[self setBackgroundColor:[UIColor blueColor]];
        path = [UIBezierPath bezierPath];
        [path setLineWidth:2.0];
    }
    
    shapeIndex= 1;
    return self;
}

- (void)drawRect:(CGRect)rect // (5)
{
    [[UIColor blackColor] setStroke];
    [path stroke];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    first_point = [touch locationInView:self];
    
    [path moveToPoint:first_point];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];

    switch (shapeIndex) {
        case 0:
            [self drawFree:p];
            break;
        case 1:
            [self drawRectangle:p];
            break;
        default:
            [self drawFree:p];
            break;
    }
    
    //[path addLineToPoint:p]; // (4)
    
    //[[UIColor blueColor] setFill];
//    path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(first_point.x, first_point.y, p.x - first_point.x, p.y - first_point.y) cornerRadius:0.0];
    //[path fill];
    
    [self setNeedsDisplay];
}

-(void) drawFree: (CGPoint)p{
    [path addLineToPoint:p];
}


- (void) drawRectangle: (CGPoint) p  {
    
    if( p.x > first_point.x && p.y > first_point.y)
    {
        path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(first_point.x, first_point.y, p.x - first_point.x, p.y - first_point.y) cornerRadius:0.0];
    }else{
        if( p.x < first_point.x && p.y< first_point.y)
        {
            path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(p.x, p.y, first_point.x - p.x, first_point.y - p.y) cornerRadius:0.0];
        }else{
            if( p.y < first_point.y)
            {
                path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(first_point.x, p.y, p.x - first_point.x, first_point.y - p.y) cornerRadius:0.0];
            }else{
                path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(p.x, first_point.y, first_point.x - p.x, p.y - first_point.y) cornerRadius:0.0];
            }
        }
    }
    
 
    last_point = p;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesMoved:touches withEvent:event];
    
    
    
    //[path addLineToPoint:last_point];
    
    switch (shapeIndex) {
        case 0:
            [_delegate updateDrawingFree:self:last_point];
            break;
        case 1:
            [_delegate updateDrawingRect:self:first_point:last_point];
            break;
        default:
            break;
    }

}

-(void) keepRectangle{

    CGPoint top_right = first_point;
    CGPoint bottom_left = first_point;
    
    top_right.x = last_point.x;
    top_right.y= first_point.y;
    
    bottom_left.x= first_point.x;
    bottom_left.y= last_point.y;
    
    [path addLineToPoint:bottom_left];
    [path addLineToPoint:last_point];
    [path addLineToPoint:top_right];
    [path addLineToPoint:first_point];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesEnded:touches withEvent:event];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
